package com.xywl.escort.view.bandcard

import android.app.Activity
import android.content.Intent
import android.graphics.Paint
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.adapter.CooperativeBandAdapter
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.FindBankBaseInfoListResp
import com.xywl.escort.widget.HorizontalDividerItemDecoration
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_cooperative_band.*
import kotlinx.android.synthetic.main.layout_empty_view.view.*
import javax.inject.Inject

@Route(path = "/xy/band/card/cooperative")
class CooperativeBandActivity : BaseActivity(), CooperativeBandContract.View {

    @Inject
    lateinit var mPresenter: CooperativeBandContract.Presenter
    private lateinit var mAdapter: CooperativeBandAdapter
    private val mList = ArrayList<FindBankBaseInfoListResp.DataBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cooperative_band)

        showBackButton()
        setTitleText("合作银行")

        mPresenter.takeView(this)
        mPresenter.findBankBaseInfoList()

        val manager = LinearLayoutManager(this)
        manager.orientation = LinearLayoutManager.VERTICAL
        recycler_cooperative_band.layoutManager = manager
        val paint = Paint()
        paint.strokeWidth = 2f
        paint.color = ContextCompat.getColor(this, R.color.line_color)
        paint.isAntiAlias = true
        recycler_cooperative_band.addItemDecoration(HorizontalDividerItemDecoration.Builder(this).paint(paint).build())

        mAdapter = CooperativeBandAdapter(R.layout.item_cooperative_band, mList)
        recycler_cooperative_band.adapter = mAdapter

        val emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty_view, null)
        val tvEmpty = emptyView.tv_empty
        tvEmpty.text = "暂无合作银行"
        mAdapter.emptyView = emptyView

        mAdapter.setOnItemClickListener { _, _, position ->
            val dataBean = mList[position]
            val intent = Intent()
            intent.putExtra("dataBean", dataBean)
            setResult(Activity.RESULT_OK, intent)
            finish()
        }
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T : Any?> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun findBankBaseInfoListSuccess(resp: FindBankBaseInfoListResp?) {
        progress_bar.visibility = View.GONE
        recycler_cooperative_band.visibility = View.VISIBLE

        if (null != resp!!.data) {
            mList.clear()
            mList.addAll(resp.data)
            mAdapter.setNewData(mList)
        }
    }

    override fun findBankBaseInfoListFailure() {
        progress_bar.visibility = View.GONE
        recycler_cooperative_band.visibility = View.VISIBLE
    }
}
